package com.aj.activity.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.aj.activity.R;
import com.aj.databases.entry.TaskInfo;
import com.aj.utils.PortraitLoaderUtil;

public class TaskInfoAdapter extends BaseAdapter {
	private boolean isSelf = false;
	private Context context;
	private List<TaskInfo> taskInfoList;
	private HashMap<Integer, View> viewCache = new HashMap<Integer, View>();
	private IButtonClick buttonClick;
	private List<TaskInfo> chooseTaskInfoList = new ArrayList<TaskInfo>();

	public interface IButtonClick {
		public void acceptTask(TaskInfo taskInfo);

		public void refuseTask(TaskInfo taskInfo);

		public void cannelTask(TaskInfo taskInfo);
	}

	public TaskInfoAdapter(Context context, List<TaskInfo> taskInfoList,
			boolean isSelf) {
		this.context = context;
		this.taskInfoList = taskInfoList;
		this.isSelf = isSelf;
	}

	@Override
	public int getCount() {
		return taskInfoList.size();
	}

	@Override
	public Object getItem(int position) {
		return taskInfoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	class ViewHolder {
		CheckBox choosetask;
		ImageView header;
		TextView nikename;
		TextView content;
		TextView datetime;
		Button accept_task_button;
		Button refuse_task_button;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = viewCache.get(position);
		ViewHolder viewHolder = null;
		final TaskInfo taskInfo = taskInfoList.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_taskinfo, null);
			viewHolder = new ViewHolder();
			viewHolder.accept_task_button = (Button) convertView
					.findViewById(R.id.accept_task_button);
			viewHolder.choosetask = (CheckBox) convertView
					.findViewById(R.id.choosetask);
			viewHolder.content = (TextView) convertView
					.findViewById(R.id.content);
			viewHolder.datetime = (TextView) convertView
					.findViewById(R.id.datetime);
			viewHolder.header = (ImageView) convertView
					.findViewById(R.id.header);
			viewHolder.nikename = (TextView) convertView
					.findViewById(R.id.nikename);
			viewHolder.refuse_task_button = (Button) convertView
					.findViewById(R.id.refuse_task_button);
			convertView.setTag(viewHolder);
			PortraitLoaderUtil.loadSrc(context, viewHolder.header, null,
					taskInfo.getImgUrl(), R.drawable.nomal_photo,
					R.drawable.nomal_photo);
			viewCache.put(position, convertView);
			if (isSelf) {
				viewHolder.accept_task_button.setVisibility(View.GONE);
				viewHolder.refuse_task_button.setText("取消");
			}
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.nikename.setText(taskInfo.getNickName());
		viewHolder.content.setText(taskInfo.getContent());
		viewHolder.datetime.setText(taskInfo.getSendTime());
		viewHolder.accept_task_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (buttonClick != null) {
					buttonClick.acceptTask(taskInfo);
				}
			}
		});
		viewHolder.choosetask
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							chooseTaskInfoList.add(taskInfo);
						} else {
							chooseTaskInfoList.remove(taskInfo);
						}
					}
				});
		viewHolder.refuse_task_button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (buttonClick != null) {
					if (isSelf) {
						buttonClick.cannelTask(taskInfo);
					} else {
						buttonClick.refuseTask(taskInfo);
					}
				}
			}
		});

		return convertView;
	}

	public IButtonClick getButtonClick() {
		return buttonClick;
	}

	public void setButtonClick(IButtonClick buttonClick) {
		this.buttonClick = buttonClick;
	}

	public List<TaskInfo> getChooseTaskInfoList() {
		return chooseTaskInfoList;
	}

	public void setChooseTaskInfoList(List<TaskInfo> chooseTaskInfoList) {
		this.chooseTaskInfoList = chooseTaskInfoList;
	}

}
